<?php
/**
 * Created by PhpStorm.
 * User: zyark
 * Date: 2018/4/2
 * Time: 11:22
 */

namespace app\index\controller;


use app\common\controller\Frontend;
use app\common\utils\Constant;
use app\common\utils\PayNotifyCallBack;
use app\index\logic\AnswerLogic;
use app\index\logic\PayLogic;
use app\index\logic\RedEnvelopeLogic;
use think\cache\driver\Redis;
use think\Loader;
use think\Log;

Loader::import('wxpay.WxPayJsApiPay');
Loader::import('wxpay.WxPayUnifiedOrder');
Loader::import('wxpay.WxPayNotify');

/**
 * 支付操作相关
 * Class Pay
 * @package app\index\controller
 */
class Pay extends Frontend
{
    /**
     * ajax缴费
     * @return \think\response\Json
     */
    public function ajax_payment(){
        $result = $this->check();
        if(!$result['success']){
            return $this->returnJson($result['msg'],$result['code']);
        }
        $house_property_id = $this->request->post('house_property_id');
        $property_charge = $this->request->post('property_charge');
        $water_charge = $this->request->post('water_charge');
        $total_money = $this->request->post('total_money');
        $user_id = $result['user_id'];
        $logic = new PayLogic();
        $result = $logic->doPay($house_property_id,$property_charge,$water_charge,$total_money,$user_id);
        $code = Constant::CODE_ERROR;
        $msg = '';
        $data = [];
        switch ($result['status']){
            case 0:
                $msg = '缴费失败';
                $data = $result['error'];
                break;
            case 1:
                $rtn_data = $result['data'];
                $msg = '创建缴费单成功';
                $description = '创建缴费单';
                $title = '创建缴费单';
                $data = $this->makeWeChatPay($rtn_data['openid'],$description,$title,$rtn_data['order_sn'],$rtn_data['money']);
                $code = Constant::CODE_SUCCESS;
                break;
            case 2:
                $msg = $result['msg'];
                break;
        }
        return $this->returnJson($msg,$code,$data);
    }

    public function weChatPayRtn(){
        Log::write('begin notify');
        $notify = new PayNotifyCallBack();
        $notify->Handle(true);
    }

    /**
     * 生成微信支付参数
     * @param $openid
     * @param $description
     * @param $title
     * @param $order_number
     * @param $money
     * @return mixed
     */
    private function makeWeChatPay($openid,$description, $title, $order_number, $money)
    {

        $tools = new \JsApiPay();
        $openId = $openid;
        //②、统一下单
        $input = new \WxPayUnifiedOrder();
        $description =mb_substr($description,0,20,'utf-8');
        $title =mb_substr($title,0,10,'utf-8');
        $input->SetBody($description);
        $input->SetAttach("test");
        $input->SetOut_trade_no($order_number);
        $input->SetTotal_fee($money*100);
//        $input->SetTotal_fee(1);
//        $input->SetTotal_fee($money);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $input->SetGoods_tag($title);
        $url = 'http://'.$_SERVER['HTTP_HOST']."/index/pay/weChatPayRtn";
        $input->SetNotify_url($url);
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openId);
        $order = \WxPayApi::unifiedOrder($input);
        $jsApiParameters = $tools->GetJsApiParameters($order);

        //获取共享收货地址js函数参数
//        $editAddress = $tools->GetEditAddressParameters();
        $data['jsApiParameters'] = $jsApiParameters;
//        $data['editAddress'] = $editAddress;
        $data['money'] = $money;
        return $data;
    }
}